using System;

namespace ScenarioFramework.Modules.DM.DataModel
{
	public class XMLDocumentBase : bOSdotNET.XML.Data.XMLObject 
	{
		public const string MAIN_NODE		= "Document";

		private const string NAME_ATTRIBUTE		= "Name";
		private const string TYPE_ATTRIBUTE		= "Type";
		private const string FILE_ATTRIBUTE		= "File";
		private const string REF_ATTRIBUTE		= "Ref";

		private const string CREATIONPOLICY_ATTRIBUTE	= "CreationPolicy";
		private const string MANAGEVERSION_ATTRIBUTE	= "ManageVersion";

		public const string NEW_CREATIONPOLICY		= "NEW";
		public const string MIGRATE_CREATIONPOLICY	= "MIGRATE";
		public const string PREVOIUS_CREATIONPOLICY	= "PREVOIUS";

		public const string NO_MANAGEVERSION		= "NO";
		public const string YES_MANAGEVERSION		= "YES";

		public const string BPMNPAGE_TYPE			= "BPMNPage";

		private string sName;
		private string sType;
		private string sRef;
		private XMLDocumentMigrates xmlDocumentMigrates;

		private XMLDocumentFile xmlFile;
		private XMLDocuments xmlDocuments;
		private XMLDocumentAttachFiles xmlDocAttachFiles;
        private ScenarioFramework.WorkflowEngine.DataModel.Common.XMLGrants xmlGrants;

		private string sCreationPolicy;
		private string sManageVersion;

		private System.Collections.Hashtable hashAttributes = new System.Collections.Hashtable();

		public XMLDocumentBase()
		{
			this.Init();
		}

		public XMLDocumentBase(System.Xml.XmlNode node)
		{
			this.Parse(node);
		}

		public XMLDocuments XMLDocuments
		{
			get { return xmlDocuments;}
			set { xmlDocuments = value;}
		}

        public ScenarioFramework.WorkflowEngine.DataModel.Common.XMLGrants XMLGrants 
		{
			get { return xmlGrants;}
			set { xmlGrants = value;}
		}

		public XMLDocumentFile XMLDocumentFile
		{
			get { return xmlFile;}
			set { xmlFile = value;}
		}

		public XMLDocumentAttachFiles XMLDocumentAttachFiles
		{
			get { return xmlDocAttachFiles;}
			set { xmlDocAttachFiles = value;}
		}

		public string Name 
		{
			get { return sName; }
			set { sName = value; }
		}

		public string Type 
		{
			get { return sType; }
			set { sType = value; }
		}

		public string Ref
		{
			get { return sRef; }
			set { sRef = value; }
		}

		public string CreationPolicy 
		{
			get { return sCreationPolicy;}
			set { sCreationPolicy = value;}
		}

		public string ManageVersion
		{
			get { return sManageVersion;}
			set { sManageVersion = value;}
		}

		public XMLDocumentMigrates XMLDocumentMigrates
		{
			get { return xmlDocumentMigrates;}
			set { xmlDocumentMigrates = value;}
		}

		override public void Init()
		{
			sName = string.Empty;
			sType = string.Empty;
			sRef = string.Empty;
			sCreationPolicy = XMLDocumentBase.PREVOIUS_CREATIONPOLICY;
			sManageVersion = XMLDocumentBase.NO_MANAGEVERSION;

			xmlDocumentMigrates = new XMLDocumentMigrates();
			xmlDocuments = new XMLDocuments(true);
			xmlFile = new XMLDocumentFile();
			xmlDocAttachFiles = new XMLDocumentAttachFiles ();
			xmlGrants = new ScenarioFramework.WorkflowEngine.DataModel.Common.XMLGrants();
		}

		override public void Parse(System.Xml.XmlNode node2Parse)
		{
			this.Init();

			System.Xml.XmlNode node;

			for ( int i=0; i<node2Parse.Attributes.Count;i++)
			{
				node = node2Parse.Attributes.Item(i);

				switch ( node.Name )
				{
					case XMLDocumentBase.NAME_ATTRIBUTE:
						sName = node.InnerText;
						break;

					case XMLDocumentBase.TYPE_ATTRIBUTE:
						sType = node.InnerText;
						break;

					case XMLDocumentBase.REF_ATTRIBUTE:
						sRef = node.InnerText;
						break;

					case XMLDocumentBase.CREATIONPOLICY_ATTRIBUTE:
						sCreationPolicy = node.InnerText;
						break;

					case XMLDocumentBase.MANAGEVERSION_ATTRIBUTE:
						sManageVersion = node.InnerText;
						break;
				}
			}

			for ( int i=0; i<node2Parse.ChildNodes.Count; i++)
			{
				node = node2Parse.ChildNodes[i];

				switch ( node.Name )
				{
					case XMLDocumentMigrates.MAIN_NODE:
						xmlDocumentMigrates.Parse (node);
						break;

					case XMLDocuments.MAIN_NODE:
						xmlDocuments.Parse(node);
						break;

					case XMLDocumentFile.MAIN_NODE:
						xmlFile.Parse(node);
						break;

					case XMLDocumentAttachFiles.MAIN_NODE:
						xmlDocAttachFiles.Parse (node);
						break;

					case ScenarioFramework.WorkflowEngine.DataModel.Common.XMLGrants.MAIN_NODE:
						xmlGrants.Parse(node);
						break;
				}
			}
		}

		override public bOSdotNET.XML.XMLNode GetXMLNode()
		{
			bOSdotNET.XML.XMLNode xmlNode = new bOSdotNET.XML.XMLNode(XMLDocumentBase.MAIN_NODE);

			xmlNode.AddNode ( xmlDocumentMigrates.GetXMLNode ());
			xmlNode.AddNode ( xmlDocuments.GetXMLNode() );
			xmlNode.AddNode ( xmlFile.GetXMLNode() );
			xmlNode.AddNode ( xmlDocAttachFiles.GetXMLNode() );
			xmlNode.AddNode ( xmlGrants.GetXMLNode() );
			
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute (XMLDocumentBase.NAME_ATTRIBUTE, sName) );
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute (XMLDocumentBase.TYPE_ATTRIBUTE, sType) );
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute (XMLDocumentBase.REF_ATTRIBUTE, sRef) );
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute (XMLDocumentBase.CREATIONPOLICY_ATTRIBUTE, sCreationPolicy) );
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute (XMLDocumentBase.MANAGEVERSION_ATTRIBUTE , sManageVersion) );
			
			return xmlNode;
		}

		public XMLDocumentBase GetDocument(string sPath)
		{
			if ( ( sPath == string.Empty ) || ( sPath == null ) )
				return this;

			if ( sPath.IndexOf (".") < 0 )
			{
				return this.XMLDocuments.Get (sPath);
			}
			else
			{
				string[] vTokens = sPath.Split ('.');

				XMLDocumentBase doc = this.XMLDocuments.Get(vTokens[0]);
				
				return doc.GetDocument(sPath.Substring ( sPath.IndexOf (".") + 1) );
			}
		}

	}
}
